import SwiftUI
import SwiftData

// SellPage
// 转一转页面

struct SellPage: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var loginInfo: [LoginInfo]
    @Query private var userInfo: [UserInfo]
    
    enum SellSelection {
        case trading
        case finish
        case all
    }
    @State private var sellSelection: SellSelection = .trading
    
    @StateObject private var sellFetcher = BookSellFetcher()
    @StateObject private var deleteBookRequester = BookDeleteBookRequester()
    
    private var filteredBookCollection: [Book_Sell] {
        sellFetcher.publishedResult.collection.filter { item in
            switch(sellSelection){
            case .trading: return item.state == 1
            case .finish: return item.state == 2
            case .all: return true
            }
        }
    }

    var body: some View {
        NavigationView {
            VStack {
                SellNewButton()
                
                Divider()
                
                HStack {
                    Spacer()
                    Button {
                        withAnimation {
                            sellSelection = .trading
                        }
                    } label: {
                        Text("交易中")
                            .foregroundColor(sellSelection == .trading ? .accent : .gray)
                    }
                    Spacer()
                    Button {
                        withAnimation {
                            sellSelection = .finish
                        }
                    } label: {
                        Text("已完成")
                            .foregroundColor(sellSelection == .finish ? .accent : .gray)
                    }
                    Spacer()
                    Button {
                        withAnimation {
                            sellSelection = .all
                        }
                    } label: {
                        Text("全部")
                            .foregroundColor(sellSelection == .all ? .accent : .gray)
                    }
                    Spacer()
                }
                .padding(.vertical, 5)
                
                Divider()
                
                List {
                    ForEach(filteredBookCollection, id:\.bookId) { item in
                        NavigationLink(destination: SellDetail(book: item)) {
                            SellListRow(book: item)
                        }
                        .swipeActions(edge: .trailing, allowsFullSwipe: true) {
                            Button {
                                showSellDeteleAlert(deleteBookRequester, bookId: item.bookId)
                            } label: {
                                Label("删除", systemImage: "trash")
                            }
                        }
                    }
                }
                .refreshable {
                    sellFetcher.fetch(userId: loginInfo.last?.userId ?? "")
                }
                .listStyle(.inset)
                .onChange(of: deleteBookRequester.requestStatus) { oldValue, newValue in
                    if(oldValue == .requesting && newValue == .finish) {
                        if(deleteBookRequester.publishedResult) {
                            sellFetcher.fetch(userId: loginInfo.last?.userId ?? "")
                        }
                    }
                }
            }
        }
        .task {
            sellFetcher.fetch(userId: loginInfo.last?.userId ?? "")
        }
    }
}

#Preview {
    SellPage()
}
